package com.lgd.factoryattendance.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lgd.factoryattendance.Bean.ProjectGuideInfo;
import com.lgd.factoryattendance.Bean.ProjectManagement;
import com.lgd.factoryattendance.R;

import java.util.List;

/**
 * @Author lgd
 * @Date 2025/1/13 10:34
 *    作业指导书listview  适配器
 */
public class ElectricGuidePdfAdapter extends BaseAdapter {
    private Context context;
    private List<ProjectGuideInfo> guideInfoList;
    private String searchText;   // 搜索关键字

    public ElectricGuidePdfAdapter(Context context, List<ProjectGuideInfo> guideInfoList) {
        this.context = context;
        this.guideInfoList = guideInfoList;
    }
    // 带搜索关键字
    public ElectricGuidePdfAdapter(Context context, List<ProjectGuideInfo> guideInfoList, String searchText) {
        this.context = context;
        this.guideInfoList = guideInfoList;
        this.searchText = searchText;
    }

    @Override
    public int getCount() {
        return guideInfoList == null ? 0 : guideInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return guideInfoList == null ? null : guideInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder holder;
        if (rowView == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.guide_pdf_listview, parent, false);
            holder = new ViewHolder();
            holder.tv_project_guide_name = rowView.findViewById(R.id.tv_project_guide_name);
            holder.tv_guide_upload_info = rowView.findViewById(R.id.tv_guide_upload_info);
            rowView.setTag(holder);
        }else {
            holder = (ViewHolder) rowView.getTag();
        }
        //  设置 数据
        ProjectGuideInfo projectGuideInfo = guideInfoList.get(position);
        String name = projectGuideInfo.getFilename();
        if(name.contains(searchText)) {
            int index = name.indexOf(searchText);
            int len = searchText.length();
            Spanned temp = Html.fromHtml(name.substring(0, index)
                    + "<u><font color=#FF0000>"
                    + name.substring(index, index + len) + "</font></u>"
                    + name.substring(index + len, name.length()));

            holder.tv_project_guide_name.setText(temp);
        }

        holder.tv_guide_upload_info.setText( "上传者： " +projectGuideInfo.getUploader()  +
                "         上传时间： " +projectGuideInfo.getUploadtime());
        return rowView;
    }

    // 定义ViewHolder类
    private static class ViewHolder{
        TextView tv_project_guide_name, tv_guide_upload_info;
    }
}
